//*************************************************************************************
//* 作    者： zouhunter
//* 创建时间： 2021-09-11 11:00:02
//* 描    述： 控制扩展

//* ************************************************************************************
using UnityEngine;

public static class ComponentExtend
{
    public static T MustComponent<T>(this GameObject target) where T : Component
    {
        var component = target.GetComponent<T>();
        if (!component)
        {
            component = target.AddComponent<T>();
        }
        return component;
    }
    public static T MustComponent<T>(this Transform target) where T : Component
    {
        return MustComponent<T>(target.gameObject);
    }
    public static void MustComponentNot<T>(this GameObject target) where T : Component
    {
        var component = target.GetComponent<T>();
        if (!component)
        {
            UnityEngine.Object.DestroyImmediate(component);
        }
    }
    public static void MustComponentNot<T>(this Transform target) where T : Component
    {
        MustComponentNot<T>(target.gameObject);
    }

    public static T FindComponent<T>(this GameObject target, string reletivePath,bool must = false) where T : Component
    {
        if (target && !string.IsNullOrEmpty(reletivePath))
        {
            var child = FindChild(target, reletivePath);
            if (child)
            {
                var component = child.GetComponent<T>();
                if (!component && must)
                    component = child.AddComponent<T>();
                return component;
            }
        }
        return null;
    }
    public static GameObject FindChild(this GameObject target, string reletivePath)
    {
        if(target)
        {
            var child = target.transform.Find(reletivePath);
            if(child)
            {
                return child.gameObject;
            }
        }
        return null;
    }

    public static bool TryFindComponent<T>(this GameObject target, string reletivePath,out T component, bool must = false) where T : Component
    {
        if (target && !string.IsNullOrEmpty(reletivePath))
        {
            var child = FindChild(target, reletivePath);
            if (child)
            {
                component = child.GetComponent<T>();
                if (!component && must)
                    component = child.AddComponent<T>();
                return component;
            }
        }
        component = null;
        return false;
    }

    public static bool TryFindChild(this GameObject target,out GameObject child, string reletivePath)
    {
        if (target)
        {
            var childTrans = target.transform.Find(reletivePath);
            if (childTrans)
            {
                child = childTrans.gameObject;
                return true;
            }
        }
        child = null;
        return false;
    }

    /// <summary>
    /// UGUI根据父对象归一化
    /// </summary>
    /// <param name="parent">Parent.</param>
    /// <param name="child">Child.</param>
    /// <param name="show">If set to <c>true</c> show.</param>
    public static void SetParentNormalize(this RectTransform parent, RectTransform child, bool show = true)
    {
        child.SetParent(parent);

        child.localScale = Vector3.one;
        child.localRotation = Quaternion.Euler(Vector3.zero);
        child.anchorMin = Vector2.zero;
        child.anchorMax = Vector2.one;
        child.offsetMin = Vector2.zero;
        child.offsetMax = Vector2.zero;
        child.localPosition = Vector3.zero;
        child.gameObject.SetActive(show);
    }

    /// <summary>
    /// 普通对象父对象归一化
    /// </summary>
    /// <param name="parent"></param>
    /// <param name="child"></param>
    /// <param name="show"></param>
    public static void SetParentNormalize(this Transform parent, Transform child, bool show = true)
    {
        if (parent == null || child == null)
            return;
        child.SetParent(parent);
        child.localScale = Vector3.one;
        child.localRotation = Quaternion.Euler(Vector3.zero);
        child.localPosition = Vector3.zero;
        child.gameObject.SetActive(show);
    }
}
